<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            /* 为小屏幕设备添加弹性布局 */
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .login-container h1 {
            text-align: center;
        }

        .login-container label {
            display: block;
            margin-bottom: 5px;
        }

        .login-container input {
            width: calc(100% - 22px); /* 减去左右边框和内边距的总宽度 */
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 3px;
            box-sizing: border-box; /* 确保内边距和边框包含在宽度内 */
        }

        .login-container button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        .login-container button:hover {
            background-color: #0056b3;
        }

        /* 媒体查询，针对小屏幕设备（如手机）进行样式调整 */
        @media (max-width: 480px) {
            .login-container {
                width: 90%; /* 缩小容器宽度以适应小屏幕 */
                padding: 15px; /* 减少内边距 */
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1>登录</h1>
        <form id="login-form">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" required>
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required>
            <button type="submit">登录</button>
        </form>
        <div id="error-message" style="color: red;"></div>
    </div>
    <script>
        document.getElementById('login-form').addEventListener('submit', (e) => {
            e.preventDefault();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            const xhr = new XMLHttpRequest();
            xhr.open('POST', '/login', true);
            xhr.setRequestHeader('Content-Type', 'application/json');

            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        try {
                            const data = JSON.parse(xhr.responseText);
                            window.location.href = '/'; // 假设跳转到仪表盘页面
                        } catch (parseError) {
                            console.error('解析响应数据出错:', parseError);
                            document.getElementById('error-message').textContent = '解析响应数据出错，请稍后重试';
                        }
                    } else {
                        try {
                            const data = JSON.parse(xhr.responseText);
                            // 登录失败，显示错误信息
                            document.getElementById('error-message').textContent = data.message;
                        } catch (parseError) {
                            console.error('解析响应数据出错:', parseError);
                            document.getElementById('error-message').textContent = '解析响应数据出错，请稍后重试';
                        }
                    }
                }
            };

            xhr.onerror = function() {
                console.error('登录请求出错:', xhr.statusText);
                document.getElementById('error-message').textContent = '请求出错，请稍后重试';
            };

            xhr.send(JSON.stringify({ username, password }));
        });
    </script>
</body>
</html>
